/*
 * @Author: flywes bg_weapon@163.com
 * @Date: 2024-11-05 18:45:44
 * @LastEditors: flywes bg_weapon@163.com
 * @LastEditTime: 2024-11-11 10:18:32
 * @FilePath: \EWM_STM8\src\motor.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "motor.h"
#include "stm8s_gpio.h"


static unsigned char status = 0x00;

void motor_init()
{
    //goio init
    GPIO_Init(MOTOR_IN_1_PORT, (GPIO_Pin_TypeDef)MOTOR_IN_1_PIN, GPIO_MODE_OUT_PP_LOW_FAST);
    GPIO_Init(MOTOR_IN_2_PORT, (GPIO_Pin_TypeDef)MOTOR_IN_2_PIN, GPIO_MODE_OUT_PP_LOW_FAST);
    GPIO_Init(MOTOR_SLEEP_PORT, (GPIO_Pin_TypeDef)MOTOR_SLEEP_PIN, GPIO_MODE_OUT_PP_LOW_FAST);
    //GPIO_Init(GPIOC, (GPIO_Pin_TypeDef)GPIO_PIN_5, GPIO_MODE_OUT_PP_LOW_FAST);

}

void motor_run(unsigned char type)
{
    switch (type)
    {
    case 0:  //停止
        GPIO_WriteLow(MOTOR_IN_1_PORT, MOTOR_IN_1_PIN);
        GPIO_WriteLow(MOTOR_IN_2_PORT, MOTOR_IN_2_PIN);
        GPIO_WriteLow(MOTOR_SLEEP_PORT, MOTOR_SLEEP_PIN);
        status = 0x00;
        break;
    case 1:  //正转 
        GPIO_WriteHigh(MOTOR_SLEEP_PORT, MOTOR_SLEEP_PIN);
        GPIO_WriteHigh(MOTOR_IN_1_PORT, MOTOR_IN_1_PIN);
        GPIO_WriteLow(MOTOR_IN_2_PORT, MOTOR_IN_2_PIN);
        status = 0x01;
        break;

    case 2:  //反转
        GPIO_WriteHigh(MOTOR_SLEEP_PORT, MOTOR_SLEEP_PIN);
        GPIO_WriteHigh(MOTOR_IN_2_PORT, MOTOR_IN_2_PIN);
        GPIO_WriteLow(MOTOR_IN_1_PORT, MOTOR_IN_1_PIN);
        status = 0x02;
        break;
    
    default:
        break;
    }
}

unsigned char motor_status(void)
{
    return status;
}